package obecto.task.container
{
	import obecto.event.TaskEvent;
	import obecto.task.IInterruptibleTask;
	import obecto.task.IPausableTask;

	public class PausableSequence extends InterruptibleSequence implements IPausableTask
	{
		
		private var _paused:Boolean;
		
		public function PausableSequence(aTasks:Array=null)
		{
			super(aTasks);
		}
		
		public function get paused():Boolean
		{
			return _paused;
		}
		public function set paused(value:Boolean):void
		{
			if (_paused != value)
			{
				if (value)
				{
					pause();
				}
				else
				{
					resume();
				}
			}
		}
		
		protected function pause():void
		{
			_paused = true;
			if (tasks[runningTaskIndex] is IPausableTask)
			{
				IPausableTask(tasks[runningTaskIndex]).paused = true;
			}
			else
			{
				IInterruptibleTask(tasks[runningTaskIndex]).interrupt();
			}
		}
		
		protected function resume():void
		{
			_paused = false;
			if (tasks[runningTaskIndex] is IPausableTask)
			{
				IPausableTask(tasks[runningTaskIndex]).paused = false;
			}
			else
			{
				IInterruptibleTask(tasks[runningTaskIndex]).start();
			}
		}
	}
}